package uutalk.netcloud.talk.base.util.thirdpart;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;

import uutalk.netcloud.talk.base.util.MediaUtils;
import uutalk.netcloud.talk.base.util.view.ToastUtil;

import java.io.File;


/**
 * Created by hujie on 16/6/21.
 */
public class MainUiManager {

    private static boolean isAsyncLogin = false;
    private static boolean downloading = false;
    private static boolean isCheck = false;




    public static void downLoad(final Context activity, String url) {
        if (downloading) {
            ToastUtil.makeToast("下载中...");
            return;
        }
        downloading = true;

        String serviceString = Context.DOWNLOAD_SERVICE;
        final DownloadManager
                downloadManager = (DownloadManager) activity.getSystemService(serviceString);

        final Uri uri = Uri.parse(url);
        DownloadManager.Request request = new DownloadManager.Request(uri);

        request.setMimeType("application/vnd.android.package-archive");

        request.setDestinationInExternalPublicDir("download", "ebook.apk");

        final long myDownloadReference = downloadManager.enqueue(request);

        IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        BroadcastReceiver receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                long reference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
                if (myDownloadReference == reference) {
                    downloading = false;
                    try {


                        DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
                        Intent updateApk = new Intent(Intent.ACTION_VIEW);
                        Uri downloadFileUri = downloadManager.getUriForDownloadedFile(myDownloadReference);
                        if (downloadFileUri != null) {
                            if (downloadFileUri.toString().startsWith("file")) {
                                updateApk.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");
                            } else {
                                String path = MediaUtils.getContentImagePath(context, downloadFileUri);
                                updateApk.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");
                            }
                        }
                        updateApk.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        activity.startActivity(updateApk);
                    } catch (Exception e) {
                        e.printStackTrace();

                    }
                }
            }
        };

        activity.registerReceiver(receiver, filter);

    }



    public static void asyncLogin() {

        UserManager.getInstance().asyncLogin();
    }


}
